package com.bjsxt.controller;

import com.bjsxt.model.Result;
import com.bjsxt.service.UserFavoriteMarketService;
import com.bjsxt.util.SpringSecurityUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * 用户收藏市场控制层
 *
 * @author ALIENWARE
 * @version v1.0.0
 * @create 06/02/2024
 **/
@RestController
@RequestMapping("/userFavorites")
@Api(tags = "用户收藏市场控制层")
public class UserFavoriteMarketController {
    /**
     * 用户收藏业务层
     */
    @Autowired
    private UserFavoriteMarketService userFavoriteMarketService;

    /**
     * 增加一个用户收藏的交易市场
     *
     * @param symbol 交易对标识
     * @param type   类型：1-数字货币；2：创新交易
     * @return 统一响应对象
     */
    @PostMapping
    @ApiOperation(value = "增加一个用户收藏的交易市场", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "symbol", value = "交易对标识", dataType = "String", paramType = "query", example = "btcgcn"),
            @ApiImplicitParam(name = "type", value = "类型：1-数字货币；2：创新交易", dataType = "Byte", paramType = "query", example = "1")
    })
    public Result<Void> add(String symbol, Byte type) {
        // 获取当前用户 ID
        Long userId = SpringSecurityUtils.getCurrentUserId();
        // 调用业务层方法进行数据的保存
        boolean flag = userFavoriteMarketService.saveByUserIdAndSumbolAndType(userId, symbol, type);
        // 如果为 true 新增成功
        if (flag) return Result.success("收藏成功");
        // 如果为 false 新增失败
        return Result.fail("收藏失败");
    }

    /**
     * 取消一个用户收藏的交易市场
     *
     * @param symbol 交易对标识
     * @return 统一响应对象
     */
    @DeleteMapping
    @ApiOperation(value = "取消一个用户收藏的交易市场", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "symbol", value = "交易对标识", dataType = "String", paramType = "query", example = "btcgcn")
    })
    public Result<Void> delete(String symbol) {
        // 获取当前用户 ID
        Long userId = SpringSecurityUtils.getCurrentUserId();
        // 调用业务层方法进行数据的保存
        boolean flag = userFavoriteMarketService.removeByUserIdAndSymbol(userId, symbol);
        // 如果为 true 新增成功
        if (flag) return Result.success("取消收藏成功");
        // 如果为 false 新增失败
        return Result.fail("取消收藏失败");
    }
}
